#include "Game.h"
#include <ctime>
#include "SDL_image.h"
#include "Assets.h"
#include "Logger.h"
#include "Box2D.h"
#include "GameConfiguration.h"


extern Logger* logger;
SDL_Renderer* gRenderer;
Game* Game::instance = NULL;
Logger *logger = Logger::getInstance();

Game::Game()
{
	_FPS = 60;
	_sleepTime = 1000 / _FPS;
	instance = this;
	logger->info("GAME-Created");
}

Game::~Game()
{
}

void Game::GameLoop()
{
	gameIsRunning = 1;

	Uint32 frameStart = 0;
	Uint32 frameEnd = 0;
	
	while (gameIsRunning)
	{
		frameStart = SDL_GetTicks();
		_GetUserInput();
		GameUpdate();
		GameDraw();
		frameEnd = SDL_GetTicks();

		SDL_Delay(_sleepTime);
	}
}


void Game::_GetUserInput()
{
	SDL_Event event; 
	while (SDL_PollEvent(&event) && gameIsRunning == 1)
	{
		_HandleKeys(&event);
	}
}

void Game::_HandleKeys(SDL_Event* event)
{

}


bool Game::GameInit()
{
	bool success = true;

	if (_InitWindowManager() == false)
	{
		return false;
	}

	if (_ReadGameConfiguration() == false)
	{
		return false;
	}

	if (_InitBox2D() == false)
	{
		return false;
	}

	if (_LoadResources() == false)
	{
		return false;
	}
	
	if (_CreateSprites() == false)
	{
		return false;
	}
	
	return success;
}

bool Game::_InitBox2D()
{
	bool success = true;

	Box2D::InitBox2D();

	return success;
}

bool Game::_InitWindowManager()
{
	_windowManager = new WindowManager();
	bool result = _windowManager->SetupWindow();
	if (result == true)
	{
		_renderer = _windowManager->GetRenderer();
		return true;
	}
	return false;
}

bool Game::_ReadGameConfiguration()
{
	bool success = true;

	return success;
}

bool Game::_LoadResources()
{
	if (!(IMG_INIT_JPG) || !(IMG_INIT_PNG))
	{
		logger->error("Could not init SDL_Image module");
		return false;
	}

	_backgroundSurface = SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, gameconfig::SCREEN_WIDTH, gameconfig::SCREEN_HEIGHT);

	if (Assets::LoadAssets() != true)
	{
		logger->error("Asset could not be loaded");
		return false;
	}
	else
	{
		return true;
	}
}

bool Game::_CreateSprites()
{
	bool success = true;
	_sprites = SpritesHolder::GetInstance();
	_sprites->CreateSprites();
	
	return success;
}


void Game::GameDraw()
{
	_GameClearSurface();
}

void Game::_GamePutToScreen()
{
	SDL_SetRenderTarget(_renderer, NULL);
	SDL_RenderCopy(_renderer, _backgroundSurface, NULL, NULL);
	SDL_RenderPresent(_renderer);
}

void Game::_GameClearSurface()
{
	SDL_RenderClear(_renderer);
	SDL_SetRenderTarget(_renderer, _backgroundSurface);
	SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 0);
	SDL_RenderClear(_renderer);
}

void Game::GameUpdate()
{
	Box2D::Step();
}

void Game::GameEnd()
{
	gameIsRunning = 0;
}

void Game::GameQuit()
{
	_sprites->DestroySprites();
	Assets::Release();
	SDL_DestroyTexture(_backgroundSurface);
	_windowManager->DestroyWindow();
}